package com.example.messagemodule.widget;

import android.content.Context;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.messagemodule.R;
import com.yilijk.base.utils.ALog;
import com.yilijk.base.utils.DataUtils;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by SHICHENG
 * <p>
 * Time on
 */
public class InquiryWaitView extends LinearLayout {

    Timer waitingTimer;
    InquityHandler handler;
    TextView timeView;
    LinearLayout inquiryWaitView;
    public InquiryWaitView(Context context) {
        super(context);
        init();
    }

    public InquiryWaitView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public InquiryWaitView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public InquiryWaitView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    void init(){
        View inquiryView = LayoutInflater.from(getContext()).inflate(R.layout.view_inquiry_wait, this);
        timeView = inquiryView.findViewById(R.id.inquiry_wait_time);
        inquiryWaitView = inquiryView.findViewById(R.id.inquiry_wait_rl);
    }

    public void setCreatTime(String timeStr){
        long chargesCreateTimeLong = DataUtils.string2long(timeStr, "yyyy-mm-dd HH:mm:ss");
        setCreateTime(chargesCreateTimeLong);
    }

    public void destoryView(){
        if (inquiryWaitView!=null){
            inquiryWaitView.setVisibility(GONE);
        }
        if (waitingTimer!=null){
            waitingTimer.cancel();
            waitingTimer = null;
        }
        if (handler!=null){
            handler.removeMessages(1);
            handler = null;
        }
    }

    public void setCreateTime(long time){
        waitingTimer = new Timer();
        handler = new InquityHandler();
        disposeTime(time);
    }

    void disposeTime(long time){
        waitingTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                long l = System.currentTimeMillis();
                long waitingTimes = l - time;
                long l1 = waitingTimes / 1000;
                long s = l1 % 60;
                long l2 = l1 / 60;
                long m = l2 % 60;
                long l3 = l2 / 60;
                long h = l3 % 24;
                Message message = new Message();
                message.what = 1;
                message.obj = "已等待 "
                        +String.format("%02d", h)+":"
                        +String.format("%02d", m)+":"
                        +String.format("%02d", s);
                handler.sendMessage(message);
            }
        },0,1000);

    }

    class InquityHandler extends Handler{
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1){
                String timeString = (String) msg.obj;
                ALog.e(timeString);
                if (timeView!=null){
                    timeView.setText(timeString);
                }
                if (inquiryWaitView!=null){
                    inquiryWaitView.setVisibility(VISIBLE);
                }
            }
        }
    }

}
